package com.gss.plugin.oauth2.handler;

import com.gss.plugin.oauth2.service.ICustomAuthService;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.expression.OAuth2ExpressionParser;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
import org.springframework.security.web.FilterInvocation;

/**
 * @author: DYang
 * @since: 2023/8/22
 * @description: 向StandardEvaluationContext 注入自定义授权对象
 */
public class CustomAuthFilterExpressionHandler extends OAuth2WebSecurityExpressionHandler {


    private final ICustomAuthService customAuthService;

    public CustomAuthFilterExpressionHandler(ICustomAuthService customAuthService) {
        this.customAuthService = customAuthService;
        setExpressionParser(new OAuth2ExpressionParser(getExpressionParser()));
    }

    @Override
    protected StandardEvaluationContext createEvaluationContextInternal(Authentication authentication,
                                                                        FilterInvocation invocation) {
        StandardEvaluationContext contextInternal = super.createEvaluationContextInternal(authentication, invocation);
        contextInternal.setVariable("customAuthService", customAuthService);
        return contextInternal;
    }

}
